package com.mgg.goods.service.store.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mgg.common.dao.DaoSupport;
import com.mgg.goods.service.store.StoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;


@Service("storeService")
public class StoreServiceImpl implements StoreService{
	
	@Autowired
	private DaoSupport dao;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> listStore(Double lng, Double lat, String storeName, Pageable page) {
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
		Map<String,Object> queryMap = new HashMap<>();
		queryMap.put("lng", lng);
		queryMap.put("lat", lat);
		if (StringUtils.isNotEmpty(storeName)) {			
			queryMap.put("store_name", storeName);
		}
		List<Map<String,Object>> storeList = dao.findForList("StoreMapper.listStore", queryMap);
		return storeList;
	}

    @Override
    public List<Map<String, Object>> listStoreByName(String name, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<Map<String,Object>> storeList = dao.findForList("StoreMapper.listStoreByName", name);
        return storeList;
    }

	@Override
	public Integer countStoreNum() {
	    Integer count = dao.findForObject("StoreMapper.countStoreNum", null);
		return count;
	}

	@Override
	public List<Map<String, Object>> listStoreByGoodsId(String goodsId, Double lng, Double lat, String storeName, Pageable page) {
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
		Map<String,Object> queryMap = new HashMap<>();
		queryMap.put("lng", lng);
		queryMap.put("lat", lat);
		if (StringUtils.isNotEmpty(storeName)) {			
			queryMap.put("store_name", storeName);
		}
		queryMap.put("goodsId", goodsId);
        List<Map<String,Object>> storeList = dao.findForList("StoreMapper.listStoreByGoodsId", queryMap);
		return storeList;
	}
}
